/* GUI CONVERTED from reupdsch.p (converter v1.75) Sat Aug 12 23:10:00 2000 */
/* reupdsch.p - REPETITIVE                                                    */
/* Copyright 1986-2002 QAD Inc., Carpinteria, CA, USA.                        */
/* All rights reserved worldwide.  This is an unpublished work.               */
/*F0PN*/ /*V8:ConvertMode=Maintenance                                         */
/*J2DG*/ /*V8:RunMode=Character,Windows */
/* REVISION: 7.3               LAST MODIFIED: 10/31/94   BY: WUG *GN77*       */
/* REVISION: 7.3               LAST MODIFIED: 11/03/94   BY: WUG *GN98*       */
/* REVISION: 7.3               LAST MODIFIED: 03/07/96   BY: jym *G1PZ*       */
/* REVISION: 9.0               LAST MODIFIED: 04/16/99   BY: *J2DG* Reetu Kapoor*/
/* REVISION: 9.1               LAST MODIFIED: 08/12/00   BY: *N0KP* myb         */


/*J2DG*/ /* GROUPED MULTIPLE FIELD ASSIGNMENTS INTO ONE AND ADDED NO-UNDO */
/*J2DG*/ /* WHEREVER MISSING AND REPLACED FIND STATEMENTS WITH FOR FIRST  */
/*J2DG*/ /* STATEMENTS FOR ORACLE PERFORMANCE.                            */

/* SUBPROGRAM TO UPDATE REPETITIVE OPERATION SCHEDULE                         */
/* ALSO SETS MRP FLAG FOR WORKORDER SITE/ITEM                                 */

     {mfdeclre.i}
     define input param cumwo_lot as character no-undo.
     define input param cumwo_op  as integer   no-undo.
     define input param qty_comp  as decimal   no-undo.

     define variable item as character no-undo.
     define variable site as character no-undo.
     define variable line as character no-undo.
/*G1PZ*  define variable routing as character. */
/*G1PZ*  define variable bom_code as character. */
     define variable qty_left     as decimal no-undo.
     define variable open_qty     as decimal no-undo.
     define variable qty_to_apply as decimal no-undo.
     define variable record_count as integer no-undo.
     define variable pointer_date as date no-undo.
     define variable new_pointer_date as date no-undo.
     define variable work_date    as date    no-undo.
     define variable key2         as character no-undo.
     define buffer sched_wr_route for wr_route.
     define variable proportion_completed as decimal no-undo.

     if qty_comp = 0 then leave.


/*J2DG** find wo_mstr where wo_lot = cumwo_lot no-lock. */
/*J2DG*/ for first wo_mstr
/*J2DG*/    fields (wo_due_date wo_line wo_lot wo_part wo_site wo_type)
/*J2DG*/    where wo_lot = cumwo_lot no-lock:
/*J2DG*/ end. /* FOR FIRST WO_MSTR */

/*J2DG*/ assign
            item = wo_part
            site = wo_site
            line = wo_line
/*G1PZ*  routing = wo_routing. */
/*G1PZ*  bom_code = wo_bom_code. */
            qty_left = qty_comp.


     {inmrp.i &part=wo_part &site=wo_site}


     /*GET THE DATE OF THE LAST RECORD  WITH  A  COMPLETED  QTY.   THE
     PURPOSE OF THIS "POINTER DATE" IS TO REDUCE THE NUMBER OF RECORDS
     READ,  WHICH  COULD  OTHERWISE  GET  QUITE  LARGE  IN THE EVENT A
     NEGATIVE QUANTITY IS APPLIED TO THE SCHEDULE.  IN  THAT  CASE  WE
     WOULD READ FROM THE END OF THE SCHEDULE BACKWARDS.*/

     key2 = item + "/" + site + "/" + line
/*G1PZ*  + "/" + routing + "/" + bom_code */
     + "/" + string(cumwo_op).

/*G1PZ*  find qad_wkfl where qad_key1 = "rpm_mstr" and qad_key2 = key2 */
/*G1PZ*/ find qad_wkfl where qad_key1 = "rps_mstr" and qad_key2 = key2
     exclusive-lock no-error.

     if available qad_wkfl then do:
        pointer_date = qad_datefld[1].
     end.
     else do:
        create qad_wkfl.

        assign
/*G1PZ*     qad_key1 = "rpm_mstr" */
/*G1PZ*/    qad_key1 = "rps_mstr"
        qad_key2 = key2.
     end.



     if qty_comp > 0 then do:
        /*NORMAL QTY PROCESSED.  APPLY FORWARD TO ANY OPEN QUANTITIES.*/

        if pointer_date = ? then pointer_date = low_date.

        for each wo_mstr
/*J2DG*/   fields (wo_due_date wo_line wo_lot wo_part wo_site wo_type)
           no-lock where wo_type      = "s"
                     and wo_part      = item
                     and wo_site      = site
                     and wo_due_date >= pointer_date
                     and wo_line      = line
/*G1PZ*              and wo_routing     = routing */
/*G1PZ*              and wo_bom_code    = bom_code */
           use-index wo_type_part,
            each wr_route
/*J2DG*/   fields (wr_lot wr_op wr_qty_comp wr_qty_ord)
           no-lock where wr_lot = wo_lot
                     and wr_op  = cumwo_op:
/*GUI*/ if global-beam-me-up then undo, leave.

           open_qty = wr_qty_ord - wr_qty_comp.

           if open_qty > 0 then do:
              qty_to_apply = min(open_qty,qty_left).

         /*UPDATE WO ROUTING REQS*/
         do for sched_wr_route:
            find sched_wr_route
            where recid(sched_wr_route) = recid(wr_route) exclusive-lock.

/*J2DG*/    assign
               wr_qty_comp          = wr_qty_comp + qty_to_apply
               proportion_completed = wr_qty_comp / wr_qty_ord.

            /*UPDATE COMPONENT REQS*/
            {gprun.i ""reupdsca.p""
            "(input wr_lot, input wr_op, input proportion_completed)"}
/*GUI*/ if global-beam-me-up then undo, leave.

         end.

/*J2DG*/ assign
            new_pointer_date = wo_due_date
            qty_left         = qty_left - qty_to_apply.

         if qty_left = 0 then leave.
           end.
        end.
/*GUI*/ if global-beam-me-up then undo, leave.

     end.
     else do:
        /*SCRAP/REJECT/NEGATIVE QTY PROCESSED.
        GO BACKWARD AND REMOVE PRIOR QTIES PROCESSED REGISTERED.*/

        if pointer_date = ? then pointer_date = hi_date.

        for each wo_mstr
/*J2DG*/   fields (wo_due_date wo_line wo_lot wo_part wo_site wo_type)
           no-lock where wo_type      = "s"
                     and wo_part      = item
                     and wo_site      = site
                     and wo_due_date <= pointer_date
                     and wo_line      = line
/*G1PZ*              and wo_routing   = routing  */
/*G1PZ*              and wo_bom_code  = bom_code */
           use-index wo_type_part,
            each wr_route
/*J2DG*/   fields (wr_lot wr_op wr_qty_comp wr_qty_ord)
           no-lock where wr_lot = wo_lot
                     and wr_op  = cumwo_op
           by wo_type descending
           by wo_part descending
           by wo_site descending
           by wo_due_date descending:
/*GUI*/ if global-beam-me-up then undo, leave.

           if wr_qty_comp > 0 then do:
              qty_to_apply = max(- wr_qty_comp, qty_left).

         /*UPDATE WO ROUTING REQS*/
         do for sched_wr_route:
            find sched_wr_route
            where recid(sched_wr_route) = recid(wr_route) exclusive-lock.

/*J2DG*/    assign
               wr_qty_comp          = wr_qty_comp + qty_to_apply
               proportion_completed = wr_qty_comp / wr_qty_ord.

            /*UPDATE COMPONENT REQS*/
            {gprun.i ""reupdsca.p""
            "(input wr_lot, input wr_op, input proportion_completed)"}
/*GUI*/ if global-beam-me-up then undo, leave.

         end.

/*J2DG*/ assign
            new_pointer_date = wo_due_date
            qty_left         = qty_left - qty_to_apply.

         if qty_left = 0 then leave.
           end.
        end.
/*GUI*/ if global-beam-me-up then undo, leave.



        /*MAY NEED TO BACK UP ONE RECORD IN CASE THE LAST PROCESSED
        EXACTLY ZEROED OUT THE QTY REQUIRED*/

        if new_pointer_date <> ? then do:
           work_date = new_pointer_date.

           for each wo_mstr
/*J2DG*/      fields (wo_due_date wo_line wo_lot wo_part wo_site wo_type)
              no-lock where wo_type      = "s"
                        and wo_part      = item
                        and wo_site      = site
                        and wo_due_date <= work_date
                        and wo_line      = line
/*G1PZ*                 and wo_routing   = routing  */
/*G1PZ*                 and wo_bom_code  = bom_code */
              use-index wo_type_part,
               each wr_route
/*J2DG*/      fields (wr_lot wr_op wr_qty_comp wr_qty_ord)
              no-lock where wr_lot = wo_lot
                        and wr_op  = cumwo_op
              by wo_type descending
              by wo_part descending
              by wo_site descending
              by wo_due_date descending:
              if wr_qty_comp <> 0 then do:
                 new_pointer_date = wo_due_date.
                 leave.
              end.
           end.
        end.
     end.


     qad_datefld[1] = new_pointer_date.
